package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IMemberCardService;
import com.younion.common.constants.SysConstants;
import com.younion.model.MyKabaoDetailResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_MemberCardVO;

public class MyCardDetailGetConsumer extends AbstractRequestConsumer {

	private IMemberCardService memberCardService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getMyCardDeatil(request, response);
	}

	public String getMyCardDeatil(HttpServletRequest request,
			HttpServletResponse response) {
		String card_id = request.getParameter("card_id");
		API_MemberCardVO api_MemberCardVO = new API_MemberCardVO();
		ResultObject resultObject = new ResultObject();
		try {
			String img_url_pre = ConfigHelper.fastGetConfig("interface",
					"img_url_pre", "http://localhost:8080/younonmember/");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Map paramMap = new HashMap();
			paramMap.put("card_id", card_id);
			api_MemberCardVO = memberCardService
					.getApiMemberCardVOByMap(paramMap);
			MyKabaoDetailResult myKabaoDetailResult = new MyKabaoDetailResult();
			if (api_MemberCardVO != null) {
				myKabaoDetailResult.setCard_id(api_MemberCardVO.getId()
						.toString());
				myKabaoDetailResult.setCard_entity_id(api_MemberCardVO.getApi_CardEntityVO().getId().toString());
				myKabaoDetailResult.setCard_name(api_MemberCardVO
						.getApi_CardEntityVO().getCardName());
				myKabaoDetailResult
						.setCard_no(api_MemberCardVO.getSchemeCode());
				myKabaoDetailResult.setCardLevel(api_MemberCardVO.getApi_CardEntityVO().getCardLevelVO().getLevelName());
				myKabaoDetailResult.setCluber_welfare(api_MemberCardVO.getApi_CardEntityVO().getWelfare());
				myKabaoDetailResult.setMyMoney(api_MemberCardVO
						.getCardBalance() == null ? "0" : api_MemberCardVO
						.getCardBalance().toString());
				myKabaoDetailResult
						.setMyScore(api_MemberCardVO.getScore() == null ? "0"
								: api_MemberCardVO.getScore().toString());
				myKabaoDetailResult.setNonactivatedPic(img_url_pre
						+ SysConstants.CARD_IMG_URL
						+ "/"
						+ api_MemberCardVO.getApi_CardEntityVO().getId()+ "/"
						+ api_MemberCardVO.getApi_CardEntityVO()
								.getNonactivatedPic());
				myKabaoDetailResult.setActivatedPic(img_url_pre
						+ SysConstants.CARD_IMG_URL
						+ "/"
						+ api_MemberCardVO.getApi_CardEntityVO().getId()+ "/"
						+ api_MemberCardVO.getApi_CardEntityVO()
								.getActivatedPic());
				myKabaoDetailResult.setTime_from(sdf.format(api_MemberCardVO.getExpiryDateFrom()));
				myKabaoDetailResult.setTime_to(sdf.format(api_MemberCardVO.getExpiryDateTo()));
			}
			resultObject.setResult(myKabaoDetailResult);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}

		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}
}
